<?php
namespace app\payqr\model;
use app\BaseModel;
use app\member\model\Member;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\IndexUserShop;
use think\facade\Db;

class CardGoods extends BaseModel
{
    protected $name = 'longbing_card_goods';
    protected $append = [
        'price_text',
        'original_price'
    ];
    public function getlist($where,$limit=0){
        $data = $this->where($where)->field('id,name,cover,price,status,unit,recommend')->order('top','desc')->limit($limit)->select()->toArray();
        $data = transImagesOne($data,['cover']);
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-23 15:24
     * @return void
     * descption:获取规格的第一个价格
     */
    public function getPriceTextAttr($value,$data){
        $spe_price = new AdminShopSpePrice();
        $price =  $spe_price->where(['status'=>1,'goods_id'=>$data['id']])->min('price');

        return $price;

    }

    /**
     * User: chenniang
     * Date: 2019-11-05 10:48
     * @return void
     * descption:获取规格的最低原价
     */
    public function getOriginalPriceAttr($value,$data){
        $o_dis[] = ['status','=',1];
        $o_dis[] = ['goods_id','=',$data['id']];
        $o_dis[] = ['original_price','<>',0];
        $original_price = Db::name('longbing_card_shop_spe_price')->where($o_dis)->min('original_price');
        return $original_price;
    }


    /**
     * @param $dis
     * @功能说明: 获取公司下的4个商品
     * @author chenniang
     * @DataTime: 2019-12-31 14:16
     */
    public function getCompanylist($dis,$uniacid = 0){

        $member_model= new Member();

        $arr = [];

        if($member_model->getAuth($uniacid)==true){

            $arr['a.is_member'] = 0;

        }
        $data = $this
            ->alias('a')
            ->join('longbing_card_company_goods b' ,'a.id = b.goods_id')
            ->join('longbing_card_user_info c','b.company_id = c.company_id')
            ->where($dis)
            ->where($arr)
            ->field(['a.id,a.name,a.cover,a.price,a.status,a.unit,a.recommend'])
            ->group('a.id')
            ->order('a.top desc,id desc')
            ->limit(4)
            ->select()
            ->toArray();
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-13 09:47
     * @功能说明:公司商品
     */
    public function companyGoodsList($uniacid,$staff_id){

        $member_model= new Member();

        $dis = [

            'a.uniacid' => $uniacid,

            'a.status'  => 1,

        ];

        if($member_model->getAuth($uniacid)==true){

            $dis['a.is_member'] = 0;

        }

        $map['a.public_goods'] = 1 ;

        $map['c.fans_id']      = $staff_id ;

        $config = longbingGetAppConfig($uniacid);


        $diss = [];
        //自选
        if($config['myshop_switch']==1){

            $user_shop = new IndexUserShop();

            $my_goods  = $user_shop->MyGoodList(['user_id'=>$staff_id,'uniacid'=>$uniacid]);

            $diss[] = ['a.id','in',$my_goods];

        }else{

            $diss[] = ['a.index_show', '=', 1];
        }

        $data = $this
            ->alias('a')
            ->join('longbing_card_company_goods b' ,'a.id = b.goods_id','left')
            ->join('longbing_card_user_info c','b.company_id = c.company_id','left')
            ->where($dis)
            ->where($diss)
            ->where(function ($query) use ($map){
                $query->whereOr($map);
            })
            ->field(['a.id,a.name,a.cover,a.price,a.status,a.unit,a.recommend'])
            ->group('a.id')
            //->order('a.top desc,id desc')
            ->order('a.recommend desc,a.top desc,a.id desc,a.update_time desc')
            ->limit(4)
            ->select()
            ->toArray();
        return $data;
    }



    public function getlistExtension($dis){
        $data = $this
            ->alias('a')
            ->join('longbing_card_company_goods b' ,'a.id = b.goods_id')
            ->join('longbing_card_user_info c','b.company_id = c.company_id')
            ->join('longbing_card_extension d','d.goods_id = a.id')
            ->where($dis)
            ->field(['a.id,a.name,a.cover,a.price,a.status,a.unit,a.recommend'])
            ->group('a.id')
            ->order('a.top desc,id desc')
            ->select()
            ->toArray();
        return $data;
    }
}